<template>
	<view class="mainContent">
		<view>
			<view class="content" v-if="dataList.length==0 ||state==0">将卡片放入读卡器中</view>
			<view v-else>
				<uni-table>
					<uni-tr v-for="(item,index) in dataList">
						<uni-td>{{item.name}}</uni-td>
						<uni-td
							:style="{color:(item.name=='车牌号' ||item.name=='运行状态')?'red':''}">{{item.content}}</uni-td>
						<uni-td>{{item.name2}}</uni-td>
						<uni-td :style="{color:(item.name2=='车牌号' ||item.name2=='运行状态')?'red':''}">{{item.content2}}
						</uni-td>
					</uni-tr>
				</uni-table>
			</view>
			<view class="button">
				<!-- <button type="primary" plain="true" size="mini" class="margin">刷身份证</button>
				<button type="primary" plain="true" size="mini" class="margin">刷IC卡</button> -->
				<button :disabled="state==0?false: true" class="margin" @click="start" disabled>开始</button>
				<button :disabled="state==1?false:true" class="margin" @click="pause">暂停</button>
				<button :disabled="state==2?false:true" class="margin" @click="resume">恢复</button>
				<button @click="negotiate" class="margin"
					:disabled="state<4||(dataFrom&&dataFrom.negotiation==1) ? true : false">协商</button>
				<button v-if="state >0 && state <4" class="margin" @click="stopJudge">结束判级</button>

				<button v-if="state == 4" class="margin" @click="submitClick">提交</button>
			</view>
		</view>
		<!-- 提交弹框 -->
		<uni-popup ref="submit_popup" type="dialog" :mask-click="false">
			<view class="popup_content">
				<scroll-view scroll-y="true" style="height: 100%;">
					<view class="popup_title"><label></label>
						<!-- 日照基地 -->
						<uni-forms :value="processForm" ref="formData" :rules="rules">
							<uni-forms-item label="身份证号/IC卡号:" required name="cardNo">
								<uni-easyinput v-model="processForm.cardNo" style="font-size: 30rpx;" type="text"
									placeholder="身份证号/IC卡号"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="介质类型（身份证）：" required name="carType">
								<uni-easyinput v-model="processForm.cardType" placeholder="请输入介质类型（身份证）" />
							</uni-forms-item>
							<uni-forms-item label="手机号：" required name="imei">
								<uni-easyinput type="number" v-model="processForm.imei" placeholder="请输入手机号" />
							</uni-forms-item>
						</uni-forms>
					</view>
					<view class="popup_button">
						<button class="buttons" @click="submitPopupCancel()">取消</button>
						<button class="buttons" @click="swipeCard()">完成</button>
					</view>
				</scroll-view>
			</view>
		</uni-popup>


	</view>
</template>

<script>
	import nfc from "@/js_sdk/hexiii-nfc/hexiii-nfc.js"
	import api from "../../common/api.js"
	export default {
		data() {
			return {
				locationId: null,
				dataList: [],
				processForm: {
					imei: null, //手机号
					cardNo: null //卡号
				},
				dataFrom: null,
				state: -1,
				isPickJin: false,
			}
		},
		onShow() {
			nfc.init({
				readCallback: this.nfcReadData
			});
			this.locationState();
			uni.$on("swipeCardResult", (data) => {
				if (data.title == this.locationId) {
					let messageType = data.type
					if (messageType == 1) {
						console.log("刷卡成功")
					} else if (messageType == 2) {
						console.log("分层结果刷新")
					} else if (messageType == 3) {
						console.log("终判结果刷新")
					} else {
						console.log("打卡机刷新")
					}
					this.locationState();

				}
			});
		},
		onLoad(option) {
			this.locationId = option.locationId;
		},
		methods: {
			stopJudge() {
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '是否结束判级',
					success: function(res) {
						if (res.confirm) {
							_this.stop();
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//暂停
			pause() {
				let that = this;
				api.postRequest("/pickJin/pause/" + that.locationId).then(res => {
					if (res.code == 0) {
						this.state = 2;
						this.$message({
							type: 'info',
							message: `成功`
						});
					} else {}
				}).catch((ex) => {
					console.info(ex)
				});
			},
			//恢复
			resume() {
				let that = this;
				api.postRequest("/pickJin/resume/" + that.locationId).then(res => {
					if (res.code == 0) {
						this.state = 1;
						this.$message({
							type: 'info',
							message: `成功`
						});
					} else {}
				}).catch((ex) => {
					console.info(ex)
				});
			},
			//判级
			stop() {
				let that = this;
				api.postRequest("/pickJin/stop", {
					poundsNo: that.dataFrom.poundsNo,
					// carNo: that.dataFrom.carNo,
					locationId: that.locationId
				}).then(res => {
					if (res.code == 0) {
						this.state = 4;
						api.showTip("成功", 1000, 'success');
					} else {
						api.showTip(res.msg, 1000, 'error');
					}
				}).catch((ex) => {
					console.info(ex)
				});
			},
			//提交
			submitClick() {
				let that = this;
				api.postRequest("/pickJin/submit/" + that.locationId + "/" + that.dataFrom.poundsNo).then(res => {
					if (res.code == 0) {
						this.state = 0;
						api.showTip("提交成功", 1000, 'success');
					} else {
						api.showTip(res.msg, 1000, 'error');
					}
				})
			},
			//协商
			negotiate() {
				api.postRequest("/pickJin/negotiate/" + this.dataFrom.poundsNo).then(data => {
					if (data.code == 0) {
						api.showTip("已请求协商", 1000, 'success');
					} else {
						api.showTip(data.msg, 1000, 'error');
					}
				})
			},
			getImage(detailsList) {
				let obj = this.setImgInfo(detailsList);
				let imgDetails = this.getImgDetails(detailsList.imgUrl2);
				let index = imgDetails.index;
				let urls = this.getImageUrl(detailsList.imgUrl);
				let url;
				let drawUrl;
				if (urls) {
					url = this.baseUrl + urls[index];
				}
				let drawUrls = this.getImageUrl(detailsList.imgDraw);
				if (drawUrls) {
					drawUrl = this.baseUrl + drawUrls[index];
				}
				return {
					imgUrl: url,
					drawUrl: drawUrl,
					imgInfo: obj,
					imgDetails: imgDetails.imgDetailObjs
				};

			},
			setImgInfo(detailsList) {
				let obj = {
					tier: detailsList.tier
				};
				obj.oldWeight = detailsList.oldWeight;
				obj.yfWeight = detailsList.yfWeight;
				obj.deduction = detailsList.deduction;
				obj.deductionRate = detailsList.deductionRate;
				obj.netWeight = detailsList.netWeight;
				obj.locationId = detailsList.locationId;
				obj.entrepotName = detailsList.entrepotName;
				obj.carNo = detailsList.carNo;
				obj.actualSpecs = detailsList.actualSpecs;
				obj.applySpecs = detailsList.applySpecs;
				obj.isEnd = detailsList.isEnd;
				obj.deGrade = detailsList.degrade;
				obj.chartData = JSON.parse(detailsList.chart);
				return obj;
			},
			getImageUrl(unHandleImgUrl) {
				unHandleImgUrl = unHandleImgUrl.replace(/\\/g, '/');
				let urls = JSON.parse(unHandleImgUrl);
				return urls;
			},
			getImgDetails(imgUrl2) {
				let imgDetailObjs = [];
				let obj = JSON.parse(imgUrl2);
				let imgDetails = obj[0].foreigns;
				let index = 0;
				//优先展示异物
				for (let i = 0; i < obj.length; i++) {
					if (obj[i].foreigns.length > 0) {
						imgDetails = obj[i].foreigns;
						index = i;
						break;
					}
				}
				//没有异物展示杂质
				if (imgDetails.length == 0) {
					console.info("没有异物展示杂质", obj)
					for (let i = 0; i < obj.length; i++) {
						if (obj[i].impures.length > 0) {
							imgDetails = obj[i].impures;
							index = i;
							break;
						}
					}
				}
				imgDetails && imgDetails.forEach(item => {
					let info = item.info.substring(1, item.info.length - 1);
					let imgDetailObj = {
						name: item.name
					};
					imgDetailObj.coordinates = info.split(',');
					imgDetailObjs.push(imgDetailObj);
				})
				return {
					"imgDetailObjs": imgDetailObjs,
					"index": index
				};
			},
			submitPopupCancel() {
				this.$refs.submit_popup.close();
			},

			//查询定级点状态
			locationState() {
				api.getRequest("/pickJin/locationState/" + this.locationId, {}).then(res => {
					console.log(res);
					if (res.code == 0) {
						this.state = res.data.locationState;
						this.isPickJin = res.data.isPickJin;
						this.isCard = res.data.isCard;

						if (res.data.locationState == 0) {
							console.log(this.locationId + "号点" + "空闲")
						} else {
							this.dataList = []
							this.dataFrom = res.data.receipt;
							let list = {};
							list.name = "过磅单号";
							list.content = this.dataFrom.poundsNo;
							list.name2 = "毛重";
							list.content2 = this.dataFrom.grossWeight;
							this.$set(this.dataList, 0, list)
							let list1 = {};
							list1.name = "车牌号";
							list1.content = this.dataFrom.carNo;
							list1.name2 = "规格";
							list1.content2 = this.dataFrom.specs;
							this.$set(this.dataList, 1, list1)

							let list2 = {};
							list2.name = "司机";
							list2.content = this.dataFrom.driverName;
							list2.name2 = "原发净重";
							list2.content2 = this.dataFrom.oldNetWeight;
							this.$set(this.dataList, 2, list2)

							let list3 = {};
							let list5 = {};
							if (this.state == 5) {
								list3.name = "物料";
								list3.content = this.dataFrom.type;
								list3.name2 = "判级结果";
								list3.content2 = this.dataFrom.intelligence;
								let list4 = {};
								list4.name = "供货商";
								list4.content = this.dataFrom.enterpriseName;
								list4.name2 = "加减价格";
								list4.content2 = this.dataFrom.dePrice;
								this.$set(this.dataList, 4, list4)
							} else {
								list3.name = "供货商";
								list3.content = this.dataFrom.enterpriseName;
								list3.name2 = "物料";
								list3.content2 = this.dataFrom.type;
							}
							this.$set(this.dataList, 3, list3)
							list5.name = "运行状态";
							list5.content2 = this.dataFrom.intelligenceJudging;
							if (this.state == 5) {
								list5.name2 = "整车扣重（吨）";
								list5.content = "判级完成，请提交！";
							} else {
								list5.name2 = "当前预计扣重（吨）";
								list5.content = "正在卸货，请等待！";
							}
							this.$set(this.dataList, 5, list5)
						}
					}
				}).catch((ex) => {
					console.info(ex)
				});
			},
			nfcReadData(cardNo) {
				if (this.state > -1 && this.state == 0) {
					//读到卡号之后
					this.processForm.cardNo = cardNo;
					console.log(this.processForm.cardNo)
					this.swipeCard(this.processForm.cardNo);
				} else {
					api.showTip("当前定级点被占用!");
				}

			},
			swipeCard() {
				console.log("swipeCard")
				api.postRequest("/pickJin/swipeCard/" + this.locationId, {
					cardNo: this.processForm.cardNo,
					cardType: "身份证或ic",
					imei: this.processForm.imei,
					bdType: 1,
					def1: 1,
					def2: 1,
					def3: 1,
				}).then(res => {
					console.log(res)
					if (res.code == 0) {
						uni.showToast({
							title: '刷卡成功',
							icon: 'success',
							duration: 1000
						});
					} else {
						console.log(res.msg)
						api.showTip(res.msg)
					}
				}).catch((ex) => {
					console.info(ex)
				});
			}
		}
	}
</script>

<style>
	.mainContent {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 80vh;
		flex-direction: column;
		padding: 20px;
		box-sizing: border-box;
	}

	.content {
		border: 1px solid #eaeaea;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 160px;
	}

	.button {
		width: 100%;
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: space-around;
		margin-top: 20px;
	}

	.margin {
		margin: 0 10px 10px 0;
	}

	.margin-left {
		margin: 0 0 10px 0;
	}
</style>